package tg.app.rj.workorder;

import java.rmi.RemoteException;
import java.util.Calendar;

import psdi.app.workorder.FldWpMatLocation;
import psdi.mbo.Mbo;
import psdi.mbo.MboRemote;
import psdi.mbo.MboSetRemote;
import psdi.mbo.MboValue;
import psdi.mbo.SqlFormat;
import psdi.server.MXServer;
import psdi.util.MXApplicationException;
import psdi.util.MXException;

public class CustFldWpMatLocation extends FldWpMatLocation {

	public CustFldWpMatLocation(MboValue mbv) throws MXException {
		super(mbv);
		// TODO Auto-generated constructor stub
	}

	@Override
	public void validate() throws MXException, RemoteException {
		// TODO Auto-generated method stub
		super.validate();
		MboValue mv = getMboValue();
	    Mbo mbo = mv.getMbo();
	    MboRemote mboremote = mv.getMbo().getOwner();
	    if (mboremote.getString("worktype").equalsIgnoreCase("LL"))
	    {
	      if ((mboremote.getString("REPORTEDBY") == null) || (mboremote.getString("REPORTEDBY") == "")) {
	        throw new MXApplicationException("workorder", "SQR");
	      }
	      mv.getMbo().setValue("issueto", mboremote.getString("REPORTEDBY"), 11L);
	    }

	    SqlFormat sqf5 = new SqlFormat(mv.getMbo().getUserInfo(), "itemnum = :1");
	    sqf5.setObject(1, "item", "itemnum", mv.getMbo().getString("itemnum"));
	    MboRemote itemMbo = mv.getMbo().getMboSet("$item", "item", sqf5.format()).getMbo(0);

	    if ((itemMbo.getBoolean("cin7")) || (itemMbo.getBoolean("cin4")))
	    {
	      SqlFormat sqf = new SqlFormat(mv.getMbo().getUserInfo(), "laborcode = :1");
	      sqf.setObject(1, "laborcraftrate", "laborcode", mv.getMbo().getString("issueto"));
	      MboSetRemote laborcraftset = MXServer.getMXServer().getMboSet("laborcraftrate", mv.getMbo().getUserInfo());
	      laborcraftset.setWhere("laborcode = '"+ mv.getMbo().getString("issueto")+"'");
	      MboRemote laborcraft = laborcraftset.getMbo(0);
	      if (laborcraft == null){
	    	  throw new MXApplicationException("workorder", "tools1");
	      }
	      if (laborcraft != null){
	    	  String craftVar = laborcraft.getString("craft");
          if (craftVar == null){
            throw new MXApplicationException("workorder", "tools1");
          }

          if (craftVar != null){
        	  MboSetRemote labitemset = MXServer.getMXServer().getMboSet("lab_item", mv.getMbo().getUserInfo());
        	  labitemset.setWhere("craft = '"+craftVar+"'  and laborcode='"+mv.getMbo().getString("issueto")+"' and itemnum='"+mv.getMbo().getString("itemnum")+"'");
    	      MboRemote labtoolMbo = labitemset.getMbo(0);  
            if (labtoolMbo == null){
              throw new MXApplicationException("workorder", "tools1");
            }
            if (labtoolMbo != null)
            {           
	        	MboSetRemote laborset = MXServer.getMXServer().getMboSet("LABOR", mv.getMbo().getUserInfo());
	        	laborset.setWhere(" laborcode='"+mv.getMbo().getString("issueto")+"' ");
	       	  	MboRemote laborMbo = laborset.getMbo(0);  	
              Calendar now = Calendar.getInstance();
              int CurrYear = (now.get(1) - laborMbo.getDate("LA15").getYear() - 1900) * 12;
              int CurrMonth = 0;
              if (now.get(5) - laborMbo.getDate("LA15").getDay() >= 0)
              {
                CurrMonth = now.get(2) + 1 - (laborMbo.getDate("LA15").getMonth() + 1);
              }
              else
              {
                CurrMonth = now.get(2) + 1 - (laborMbo.getDate("LA15").getMonth() + 1) - 1;
              }

              double ljlybz = (CurrYear + CurrMonth) / labtoolMbo.getInt("timelimit") + 1;

              MboSetRemote itemMboSet = mv.getMbo().getMboSet("$itemnum", "matusetrans", "itemnum='" + mv.getMbo().getString("itemnum") + "' and muse01='" + mv.getMbo().getString("issueto") + "' and actualdate>=(select LA15 from labor where laborcode='" + mv.getMbo().getString("issueto") + "')");
              double sumnum = 0.0D;
              if (!itemMboSet.isEmpty())
              {
                sumnum = Math.abs(itemMboSet.sum("quantity"));
              }
              double ycqty = labtoolMbo.getDouble("qty");
              if (ljlybz*ycqty - labtoolMbo.getDouble("CSQTY") - sumnum <=0.0D )
              {
            	throw new MXApplicationException("workorder", "tools1");
              }

            //  mv.getMbo().setValue("wpm12", ljlybz - labtoolMbo.getDouble("CSQTY") + sumnum, 11L);
            }
          }
        }
	   }
	}
}
